package fr.jade.fraclite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.objectweb.fractal.api.Interface;
import org.objectweb.fractal.api.Type;
import org.objectweb.fractal.api.factory.InstantiationException;

import fr.jade.fraclite.api.Controller;

/**
 * A context used to initialize {@link Controller} objects.
 * @see Controller#initFcController initFcController
 */
public class InitializationContext {

  /**
   * The type of the component to which the controller object belongs.
   */
  public Type type;

  /**
   * The controllers objects of the component to which the controller object
   * belongs.
   */
  public List<Interface> controllers;

  /**
   * The external interfaces of the component to which the controller object
   * belongs.
   */
  public Map<String, Interface> interfaces;

  /**
   * The external interfaces of the component to which the controller object
   * belongs.
   */
  public Map<String, Interface> internalInterfaces;

  /**
   * The content of the component to which the controller object belongs.
   */
  public Object content;

  /**
   * Miscellaneous additional context information.
   */
  public Object hints;

  /**
   * Constructs an uninitialized {@link InitializationContext} object.
   */
  public InitializationContext () {
    controllers = new ArrayList<Interface>();
    interfaces = new HashMap<String, Interface>();
    internalInterfaces = new HashMap<String, Interface>();
  }

  /**
   * Returns the component interface of the {@link #interfaces interfaces} map
   * whose name is given.
   *
   * @param name the name of a component interface.
   * @return the interface whose name is given.
   * @throws InstantiationException if there is no such component interface.
   */
  public Interface getInterface (final String name) throws InstantiationException {
    Interface itf = getOptionalInterface(name);
    if (itf == null) {
      throw new InstantiationException("Cannot find the required interface '" + name + '\'');
    }
    return itf;
  }

  /**
   * Returns the component interface of the {@link #interfaces interfaces} map
   * whose name is given.
   *
   * @param name the name of a component interface.
   * @return the interface whose name is given, or <tt>null</tt> if there is no
   *      such component interface.
   */
  public Interface getOptionalInterface (final String name) {
    Interface itf = interfaces.get(name);
    return itf;
  }
}
